package demo1;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

class Node {
    public int val;
    public Node next;

    public Node(int val) {
        this.val = val;
    }

}
//单链表实现队列
public class MyQueue {
    public Node head;
    public Node last;

    public void offer(int val) {
        Node newNode = new Node(val);
        if (this.head == null) {
            this.head = newNode;
            this.last = newNode;
        } else {
            this.last.next = newNode;
            this.last = this.last.next;
        }

    }

    public int poll() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        int oldVal = this.head.val;
        this.head = this.head.next;
        return oldVal;
    }

    public int peek() {
        if (isEmpty()) {
            throw new RuntimeException("栈为空");
        }
        return this.head.val;
    }


    private boolean isEmpty() {
        return this.head == null;
    }



    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        myQueue.offer(29);
        myQueue.offer(34);
        myQueue.offer(22);
        System.out.println(myQueue.poll());
        System.out.println(myQueue.poll());
        System.out.println(myQueue.poll());
        System.out.println(myQueue.peek());


    }
}
